﻿using System;
using System.Web;
using Dost.Khcn.Biz;

namespace Dost.Khcn.Web
{
    public partial class Download : System.Web.UI.Page
    {
        private static ChuyenGiaFileBiz _chuyenGiaFileBiz;
        private static ChuyenGiaFileBiz ChuyenGiaFileManager
        {
            get { return _chuyenGiaFileBiz ?? (_chuyenGiaFileBiz = new ChuyenGiaFileBiz()); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var strId = Request.Params["id"];
            if (string.IsNullOrEmpty(strId))
                return;
            int id;
            if (!int.TryParse(strId, out id))
                return;
            if (id <= 0)
                return;

            var file = ChuyenGiaFileManager.GetFileById(id);
            if (file == null)
                return;

            // download
            var bytes = file.FileContent;
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = file.ContentType;
            Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(file.FileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }
    }
}